package com.itqf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itqf.bean.DtsAdmin;
import com.itqf.dto.DtsSearchDto;
import com.itqf.mapper.DtsAdminMapper;
import com.itqf.service.DtsAdminService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * projectName: shop_manage
 *
 * @author: MaTianWei
 * time: 2021/7/13 14:27
 * description:
 */
@Service
public class DtsAdminServiceImpl implements DtsAdminService {
    @Autowired
    private DtsAdminMapper dtsAdminMapper;
    public static final Logger logger = LoggerFactory.getLogger(DtsAdminServiceImpl.class);
    @Override
    public DtsAdmin queryByUsername(String username) {
        if (username == null) {
            //warn 警告
            logger.warn("[账号查看业务]username为null");
            return null;
        }
        DtsAdmin dtsAdmin = dtsAdminMapper.queryByUserName(username);
        return dtsAdmin;
    }






    @Override
    public Map findAdminByPage(DtsSearchDto dto) {
        logger.info("[管理员查询]业务开始调用获取数据为:{}",dto);
        //1·分页插件启动
        PageHelper.startPage(dto.getPage(),dto.getLimit());
        //2·数据库查询
        List<DtsAdmin> adminList = dtsAdminMapper.queryAdminByPage(dto);
        //3·pageInfo封装
        PageInfo<DtsAdmin> pageInfo = new PageInfo<>(adminList);
        //4·封装返回结果数据
        Map data = new HashMap();
        data.put("total",pageInfo.getTotal());
        data.put("items",pageInfo.getList());
        logger.info("[管理员查询]业务调用完毕,获取数据为:{}",data);
        return data;
    }






    //新注册用户和数据库进行比对
    @Override
    public boolean checkUserName(String username) {
        Long count = dtsAdminMapper.queryCountByUserName(username);
        logger.debug("[用户查询]username={}的账号已存在数量为:{}",username,count);
        return count >= 1;
    }





    //保存用户
    @Override
    public DtsAdmin saveAdmin(DtsAdmin admin) {
        //1·完善admin的数据 补全没有的数据
        admin.setAddTime(new Date());
        admin.setDeleted(false);
        //2·需要修改的数据
        logger.debug("[保存管理员]业务内部获取用户的密码为:{}",admin.getPassword());
        //3·插入数据库
        int rows = dtsAdminMapper.insertAdmin(admin);
        //4·返回
        return admin;
    }





    //修改数据
    @Override
    public int updateAdmin(DtsAdmin dtsAdmin) {
        dtsAdmin.setUpdateTime(new Date());
        int rows = dtsAdminMapper.updateById(dtsAdmin);
        logger.debug("[修改管理员]业务修改影响数据的形象行数rows:{}",rows);
        return rows;
    }





    //删除
    @Override
    public int removeAdminById(Integer id) {
        int rows = dtsAdminMapper.deleteByPrimaryKey(id);
        return rows;
    }
}
